package org.code.boot.service.boot.impl;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import cn.hutool.core.collection.CollUtil;
import java.util.stream.Collectors;
import org.code.boot.common.exception.ServiceException;
import org.code.boot.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.code.boot.mapper.boot.BizHotelOrderMapper;
import org.code.boot.domain.boot.BizHotelOrder;
import org.code.boot.service.boot.IBizHotelOrderService;
import org.code.boot.service.boot.IBizHotelService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 酒店订单Service业务层处理
 * 
 * @author coder
 * @date 2025-08-28
 */
@Service
public class BizHotelOrderServiceImpl implements IBizHotelOrderService 
{
    @Autowired
    private BizHotelOrderMapper bizHotelOrderMapper;

    @Autowired
    private IBizHotelService bizHotelService;


    /**
     * 查询酒店订单
     * 
     * @param hotelOrderId 酒店订单主键
     * @return 酒店订单
     */
    @Override
    public BizHotelOrder selectBizHotelOrderByHotelOrderId(Long hotelOrderId)
    {
        return bizHotelOrderMapper.selectBizHotelOrderByHotelOrderId(hotelOrderId);
    }

    /**
     * 查询酒店订单列表
     * 
     * @param bizHotelOrder 酒店订单
     * @return 酒店订单
     */
    @Override
    public List<BizHotelOrder> selectBizHotelOrderList(BizHotelOrder bizHotelOrder)
    {
        return bizHotelOrderMapper.selectBizHotelOrderList(bizHotelOrder);
    }

    /**
     * 新增酒店订单
     * 
     * @param bizHotelOrder 酒店订单
     * @return 结果
     */
    @Override
    public int insertBizHotelOrder(BizHotelOrder bizHotelOrder)
    {
        bizHotelOrder.setCreateTime(DateUtils.getNowDate());
        return bizHotelOrderMapper.insertBizHotelOrder(bizHotelOrder);
    }

    /**
     * 修改酒店订单
     * 
     * @param bizHotelOrder 酒店订单
     * @return 结果
     */
    @Override
    public int updateBizHotelOrder(BizHotelOrder bizHotelOrder)
    {
        bizHotelOrder.setUpdateTime(DateUtils.getNowDate());
        return bizHotelOrderMapper.updateBizHotelOrder(bizHotelOrder);
    }

    /**
     * 批量删除酒店订单
     * 
     * @param hotelOrderIds 需要删除的酒店订单主键
     * @return 结果
     */
    @Override
    public int deleteBizHotelOrderByHotelOrderIds(Long[] hotelOrderIds)
    {
        // 检查数据表是否被其他表引用
        List<Long> idList = Arrays.asList(hotelOrderIds);
        if (isDataTableReferenced(idList))
        {
            throw new ServiceException("该酒店订单中有数据正在被其他表引用，无法删除");
        }
        return bizHotelOrderMapper.deleteBizHotelOrderByHotelOrderIds(hotelOrderIds);
    }

    /**
     * 删除酒店订单信息
     * 
     * @param hotelOrderId 酒店订单主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBizHotelOrderByHotelOrderId(Long hotelOrderId)
    {
        // 检查数据表是否被其他表引用
        List<Long> idList = Arrays.asList(hotelOrderId);
        if (isDataTableReferenced(idList))
        {
            throw new ServiceException("该酒店订单正在被其他数据引用，无法删除");
        }
        
        // 先获取订单信息用于恢复库存
        BizHotelOrder order = bizHotelOrderMapper.selectBizHotelOrderByHotelOrderId(hotelOrderId);
        
        // 删除订单
        int result = bizHotelOrderMapper.deleteBizHotelOrderByHotelOrderId(hotelOrderId);
        
        // 恢复房间库存 - 增加可用房间数
        if (result > 0 && order != null) {
            bizHotelService.increaseAvailableRooms(order.getHotelId(), 1);
        }
        
        return result;
    }
    /**
     * 获得酒店订单列表
     *
     * @param ids 酒店订单编号数组
     * @return 酒店订单列表
     */
    @Override
    public List<BizHotelOrder> getBizHotelOrderList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        return bizHotelOrderMapper.selectBizHotelOrderListByIds(ids);
    }

    /**
     * 根据用户ID列表统计酒店订单数量
     *
     * @param userIds 用户ID数组
     * @return 酒店订单数量
     */
    @Override
    public Long countByUserIds(Collection<Long> userIds) {
        if (CollUtil.isEmpty(userIds)) {
            return 0L;
        }
        return bizHotelOrderMapper.countByUserIds(userIds);
    }

    /**
     * 根据酒店ID列表统计酒店订单数量
     *
     * @param hotelIds 酒店ID数组
     * @return 酒店订单数量
     */
    @Override
    public Long countByHotelIds(Collection<Long> hotelIds) {
        if (CollUtil.isEmpty(hotelIds)) {
            return 0L;
        }
        return bizHotelOrderMapper.countByHotelIds(hotelIds);
    }

    
    /**
     * 检查数据表是否被其他表引用
     * 
     * @param ids 酒店订单主键列表
     * @return 是否被引用
     */
    private boolean isDataTableReferenced(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return false;
        }
        
        // 当前数据表没有被其他表引用，可以安全删除
        return false;
    }

    /**
     * 创建酒店订单（包含库存更新）
     * 
     * @param bizHotelOrder 酒店订单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int createHotelOrder(BizHotelOrder bizHotelOrder) {
        // 计算入住天数（默认为1天）
        int roomCount = 1;
        
        // 检查房间库存并减少可用房间数
        boolean stockReduced = bizHotelService.decreaseAvailableRooms(bizHotelOrder.getHotelId(), roomCount);
        if (!stockReduced) {
            throw new ServiceException("房间库存不足，订单创建失败");
        }
        
        // 创建订单
        bizHotelOrder.setCreateTime(DateUtils.getNowDate());
        int result = bizHotelOrderMapper.insertBizHotelOrder(bizHotelOrder);
        
        if (result <= 0) {
            throw new ServiceException("订单创建失败");
        }
        
        return result;
    }
}
